﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace WebApplication.Admins
{
    public partial class SanPhams : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateControls();
            }
        }

        private void PopulateControls()
        {
            List<Entities.SanPham> data = SanPhamBLL.LayTatCa();
            repDSSanPham.DataSource = data;
            repDSSanPham.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
           base.OnInit(e);
           btnXoa.Click += new EventHandler(btnXoa_Click);
           repDSSanPham.ItemCommand += new RepeaterCommandEventHandler(repDSSanPham_ItemCommand);
        }

        void repDSSanPham_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "CapNhatHienThi")
            {
                string []args = e.CommandArgument.ToString().Split('_');
                SanPhamBLL.SanPham_CapNhatHienThi(args[0], !Boolean.Parse(args[1]));
                PopulateControls();
            }
        }

        void btnXoa_Click(object sender, EventArgs e)
        {
            string stringId = Request.Form["cid"] ?? "";
            if (stringId != "")
            {
                foreach (string id  in stringId.Split(','))
                {
                    SanPhamBLL.Xoa(id);
                    PopulateControls();
                }
            }
        }

    }
}